package com.acmen.easyapi.generator;

import com.acmen.easyapi.annotation.Param;
import com.github.javafaker.Faker;
import com.power.common.util.DateTimeUtil;
import com.power.common.util.IDCardUtil;
import com.power.common.util.RandomUtil;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.*;

/**
 * 数字获取器
 *
 * @author acmen
 * @date 2022-04-06
 */
@Component
public class CommonDataGenerator implements DataGenerator {

    private static Map<String, Generator> fieldValue = new LinkedHashMap<>();
    static {
        Faker enFaker = new Faker(new Locale(Locale.CHINA.getLanguage()));
        Faker faker = new Faker(new Locale(Locale.CHINA.getLanguage()));
        fieldValue.put("id-string", ()->{return RandomUtil.randomInt(1, 200);});
        fieldValue.put("nickname-string", ()->{return enFaker.name().username();});
        fieldValue.put("hostname-string", ()->{return faker.internet().ipV4Address();});
        fieldValue.put("author-string", ()->{return faker.book().author();});
        fieldValue.put("url-string", ()->{return faker.internet().url();});
        fieldValue.put("username-string", ()->{return faker.name().username();});
        fieldValue.put("page-int", ()->{return "1";});
        fieldValue.put("page-integer", ()->{return "1";});
        fieldValue.put("domain-string", ()->{return faker.internet().domainName();});
        fieldValue.put("phone-string", ()->{return faker.phoneNumber().cellPhone();});
        fieldValue.put("telephone-string", ()->{return faker.phoneNumber().phoneNumber();});
        fieldValue.put("address-string", ()->{return faker.address().fullAddress().replace(",", "，");});
        fieldValue.put("ip-string",()->{return faker.internet().ipV4Address();});
        fieldValue.put("ipv4-string",()->{return faker.internet().ipV4Address();});
        fieldValue.put("ipv6-string",()->{return faker.internet().ipV6Address();});
        fieldValue.put("company-string",()->{return faker.company().name();});
        fieldValue.put("timestamp-long",()->{return System.currentTimeMillis();});
        fieldValue.put("timestamp-string",()->{return DateTimeUtil.dateToStr(new Date(), DateTimeUtil.DATE_FORMAT_SECOND);});
        fieldValue.put("time-long",()->{return System.currentTimeMillis();});
        fieldValue.put("time-string",()->{return DateTimeUtil.dateToStr(new Date(), DateTimeUtil.DATE_FORMAT_SECOND);});
        fieldValue.put("code-string",()->{return String.valueOf(RandomUtil.randomInt(100, 99999));});
        fieldValue.put("message-string",()->{return "success,fail".split(",")[RandomUtil.randomInt(0, 1)];});
        fieldValue.put("date-string",()->{return DateTimeUtil.dateToStr(new Date(), DateTimeUtil.DATE_FORMAT_DAY);});
        fieldValue.put("date-date",()->{return DateTimeUtil.dateToStr(new Date(), DateTimeUtil.DATE_FORMAT_DAY);});
        fieldValue.put("begintime-date",()->{return DateTimeUtil.dateToStr(new Date(), DateTimeUtil.DATE_FORMAT_DAY);});
        fieldValue.put("endtime-date",()->{return DateTimeUtil.dateToStr(new Date(), DateTimeUtil.DATE_FORMAT_DAY);});
        fieldValue.put("time-localtime",()->{return DateTimeUtil.long2Str(System.currentTimeMillis(), DateTimeUtil.DATE_FORMAT_SECOND);});
        fieldValue.put("state-int",()->{return RandomUtil.randomInt(0, 10);});
        fieldValue.put("state-integer",()->{return RandomUtil.randomInt(0, 10);});
        fieldValue.put("flag-int",()->{return RandomUtil.randomInt(0, 10);});
        fieldValue.put("flag-integer",()->{return RandomUtil.randomInt(0, 10);});
        fieldValue.put("flag-boolean",()->{return new Random().nextBoolean();});
        fieldValue.put("flag-Boolean",()->{return new Random().nextBoolean();});
        fieldValue.put("idcard-string",()->{return IDCardUtil.getIdCard();});
        fieldValue.put("sex-int",()->{return RandomUtil.randomInt(0, 2);});
        fieldValue.put("sex-integer",()->{return RandomUtil.randomInt(0, 2);});
        fieldValue.put("gender-int",()->{return RandomUtil.randomInt(0, 2);});
        fieldValue.put("gender-integer",()->{return RandomUtil.randomInt(0, 2);});
        fieldValue.put("limit-int", ()->{return new Random().nextInt(100);});
        fieldValue.put("limit-integer",()->{return new Random().nextInt(100);});
        fieldValue.put("size-int",()->{return 10;});
        fieldValue.put("size-integer",()->{return 10;});
        fieldValue.put("offset-int",()->{return new Random().nextInt(100);});
        fieldValue.put("offset-integer",()->{return new Random().nextInt(100);});
        fieldValue.put("offset-long",()->{return new Random().nextInt(100);});
        fieldValue.put("version-string", ()->{return enFaker.app().version();});
    }
    @Override
    public boolean matches(Field field, Param param) {
        // 根据字段名称和类型生成一个唯一的标识符
        String key = field.getName()+"-"+field.getType().getSimpleName().toLowerCase();
        // 检查fieldValue是否包含该唯一标识符
        return fieldValue.containsKey(key);

    }

    @Override
    public Object generate(Field field, Param param) {
        // 生成属性名和类型名的组合键
        String key = field.getName()+"-"+field.getType().getSimpleName().toLowerCase();
        // 使用组合键从字段值中获取对应的方法生成器对象
        return fieldValue.get(key).gen();

    }

    /**
     * 数据获取说明
     *
     * @return
     */
    @Override
    public String getNote() {
        String note = "说不清楚，太多了！";
        return note;
    }

    interface Generator{
        public Object gen();
    }
}
